package com.tianqu.tms.www.car.controller;

import java.util.List;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.Car;
import com.tianqu.tms.entity.CarImage;
import com.tianqu.tms.entity.CarStyle;
import com.tianqu.tms.entity.Codes;
import com.tianqu.tms.entity.Member;
import com.tianqu.tms.entity.Vendor;
import com.tianqu.tms.www.car.actionform.CarSearchInputActionForm;
import com.tianqu.tms.www.car.viewhelper.CarCreationInputViewHelper;

public class CarSearchInputTopController extends TmsController {

	private Logger logger = Logger.getLogger(CarSearchInputTopController.class);
	public static final String LICENSEPLATE = "licensePlate";

	@SuppressWarnings("unchecked")
	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = TxController.getTxController();

		// 添加事务提交异常捕获
		try {
			controller.begin();
			CarSearchInputActionForm form = (CarSearchInputActionForm) uc
					.getInputForm();

			Car car = Car.getInstance();
			// 根据VIN码查询车辆信息
			car = car.findCarByVin(form.getVin());
			CarCreationInputViewHelper carCreationInputViewHelper = new CarCreationInputViewHelper();
			if (car.getLicensePlate() != null
					&& !"".equals(car.getLicensePlate())) {
				if (uc.getAttribute(TmsConstants.VIEWHELP) != null) {
					carCreationInputViewHelper = (CarCreationInputViewHelper) uc
							.getAttribute(TmsConstants.VIEWHELP);
				}
			}

			Codes code = Codes.getInstance();
			carCreationInputViewHelper.setCarTypeList(code
					.getCodeslistByItemId(Codes.VEHICLE_TYPE));// 车辆类型
			carCreationInputViewHelper.setColorList(code
					.getCodeslistByItemId(Codes.VEHICLE_COLOR));// 车辆颜色
			carCreationInputViewHelper.setCarUseList(code
					.getCodeslistByItemId(Codes.USER_NATURE));// 使用性质list
			carCreationInputViewHelper.setCardTypeList(code
					.getCodeslistByItemId(Codes.PLATE_TYPE));// 号牌种类list
			carCreationInputViewHelper.setFuelTypeList(code
					.getCodeslistByItemId(Codes.FUEL_TYPE));// 燃料种类list
			carCreationInputViewHelper.setCountryStyleList(code
					.getCodeslistByItemId(Codes.VEHICLE_COUNTRY));// 国别list
			carCreationInputViewHelper.setAttributeList(code
					.getCodeslistByItemId(Codes.ATTACHMENT_PROPERTY));// 车辆状态list'
			carCreationInputViewHelper.setOwnerTypeList(code
					.getCodeslistByItemId(Codes.CAR_OWNER));// 车主类型list
			Member menber = Member.getInstance();
			carCreationInputViewHelper.setMemberList(menber.findAllMemInfo());// 会员类型
			// 车辆品牌
			Vendor vendor = Vendor.getInstance();
			carCreationInputViewHelper
					.setVendorList(vendor.findAllVendorInfo());// 车主类型list
	

			// 如果能查出车辆的信息进入到车辆的修改页面
			if (car.getLicensePlate() != null && "" != car.getLicensePlate()) {
				setViewhelperFromEntity(carCreationInputViewHelper, car, uc);
			} else {
				carCreationInputViewHelper.setVin(form.getVin());
				// 上传图片部分，判断某部位是否有图片

				carCreationInputViewHelper.setImg1("");
				carCreationInputViewHelper.setImg2("");
				carCreationInputViewHelper.setImg3("");
				carCreationInputViewHelper.setImg4("");
				carCreationInputViewHelper.setImg5("");
				carCreationInputViewHelper.setImg6("");
				carCreationInputViewHelper.setImg7("");
				carCreationInputViewHelper.setImg8("");

			}
			uc.setAttribute(TmsConstants.VIEWHELP, carCreationInputViewHelper);
			uc.setAttribute(LICENSEPLATE, car.getLicensePlate());
			controller.commit();
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}

		logger.debug(this.getClass().getName() + "#execute was completed.");
	}

	/**
	 * 从Entity 给viewHelper 赋值
	 * 
	 * @param viewHelper
	 * @param car
	 * @throws TmsDaoException
	 */
	private void setViewhelperFromEntity(CarCreationInputViewHelper viewHelper,
			Car car, UserContext uc) throws TmsDaoException {
		Codes code = Codes.getInstance();
		CarStyle carStyle = CarStyle.getInstance();
		viewHelper.setAttribute(code.getItemValueByItemIdAndItemCode(
				Codes.ATTACHMENT_PROPERTY, car.getAttribute()));
		viewHelper.setBrandId(car.getBrandId());
		viewHelper.setCardType(code.getItemValueByItemIdAndItemCode(
				Codes.PLATE_TYPE, car.getCardType()));
		viewHelper.setLicensePlate(car.getLicensePlate());
		viewHelper.setCarOwner(car.getCarOwner());
		viewHelper.setCarOwnerIdCardNo(car.getCarOwnerIdCardNo());
		viewHelper.setCarType(code.getItemValueByItemIdAndItemCode(
				Codes.VEHICLE_TYPE, car.getCarType()));
		viewHelper.setCarUse(code.getItemValueByItemIdAndItemCode(
				Codes.USER_NATURE, car.getCarUse()));
		viewHelper.setColor(code.getItemValueByItemIdAndItemCode(
				Codes.VEHICLE_COLOR, car.getColor()));
		viewHelper.setCompanyContactPerson(car.getCompanyContactPerson());
		viewHelper.setCompanyContactPhone(car.getCompanyContactPhone());
		viewHelper.setCountryStyle(code.getItemValueByItemIdAndItemCode(
				Codes.VEHICLE_COUNTRY, car.getCountryStyle()));
		if (car.getDisplacement() == 0) {
			viewHelper.setDisplacement("");
		} else {
			viewHelper.setDisplacement(Float.toString(car.getDisplacement()));
		}

		viewHelper.setEngine(car.getEngine());
		viewHelper.setFuelType(code.getItemValueByItemIdAndItemCode(
				Codes.FUEL_TYPE, car.getFuelType()));
		viewHelper.setGearbox(code.getItemValueByItemIdAndItemCode(
				Codes.GEARBOX_TYPE, car.getGearbox()));
		viewHelper.setJqx(car.getJqx());
		if (car.getLoadquality() == 0) {
			viewHelper.setLoadquality("");
		} else {
			viewHelper.setLoadquality(Integer.toString(car.getLoadquality()));
		}

		viewHelper.setCarName(carStyle.getCarStyleRealNameById(car
				.getCarStyleId()));
		viewHelper.setCarStyleId(car.getCarStyleId());
		//
		Member member = Member.getInstance();
		viewHelper.setMemberId(member.getMemNameByMemId(car.getMemberId()));
		if (car.getMileage() == 0) {
			viewHelper.setMileage("");
		} else {
			viewHelper.setMileage(Integer.toString(car.getMileage()));
		}

		viewHelper.setModelCode(car.getModelCode());
		viewHelper.setOwnerAddress(car.getOwnerAddress());
		viewHelper.setOwnerCardNo(car.getOwnerCardNo());
		viewHelper.setOwnerOrgNo(car.getOwnerOrgNo());
		viewHelper.setOwnerPhone(car.getOwnerPhone());
		viewHelper.setOwnerType(code.getItemValueByItemIdAndItemCode(
				Codes.CAR_OWNER, car.getOwnerType()));
		if (car.getPassenger() == 0) {
			viewHelper.setPassenger("");
		} else {
			viewHelper.setPassenger(Integer.toString(car.getPassenger()));
		}
		if (car.getPrice() == 0) {
			viewHelper.setPrice("");
		} else {
			viewHelper.setPrice(Double.toString(car.getPrice()));
		}

		viewHelper.setRegDate(car.getRegDate());
		viewHelper.setRegisterCardNo(car.getRegisterCardNo());
		viewHelper.setSendCardDate(car.getSendCardDate());
		viewHelper.setSerial(car.getSerial());
		viewHelper.setUpdateTime(car.getUpdateTime());
		viewHelper.setUpdateUserId(car.getUpdateUserId());
		viewHelper.setVendorCode(car.getVendorCode());
		viewHelper.setVin(car.getVin());
		viewHelper.setYearCarefulPeriod(car.getYearCarefulPeriod());
		viewHelper.setId(car.getcId());
		// 进入修改页面则从数据库中读出图片信息
		CarImage imgEntity = CarImage.getInstance();
		List<CarImage> imgList = imgEntity.getCarImgInfoBySerial(car.getcId());
		uc.setAttribute("imgList", imgList);
		if (imgList != null) {
			// 将list中的数据取出set到viewhelp中
			for (int i = 0; i < imgList.size(); i++) {
				CarImage entity = (CarImage) imgList.get(i);
				if (entity.getImgKey().equals("1")) {
					viewHelper.setImg1(entity.getImgPath());
				}
				if (entity.getImgKey().equals("2")) {
					viewHelper.setImg2(entity.getImgPath());
				}
				if (entity.getImgKey().equals("3")) {
					viewHelper.setImg3(entity.getImgPath());
				}
				if (entity.getImgKey().equals("4")) {
					viewHelper.setImg4(entity.getImgPath());
				}
				if (entity.getImgKey().equals("5")) {
					viewHelper.setImg5(entity.getImgPath());
				}
				if (entity.getImgKey().equals("6")) {
					viewHelper.setImg6(entity.getImgPath());
				}
				if (entity.getImgKey().equals("7")) {
					viewHelper.setImg7(entity.getImgPath());
				}
				if (entity.getImgKey().equals("8")) {
					viewHelper.setImg8(entity.getImgPath());
				}
			}

		} else {
			// 在list为空的情况下赋一个初值
			viewHelper.setImg1("");
			viewHelper.setImg2("");
			viewHelper.setImg3("");
			viewHelper.setImg4("");
			viewHelper.setImg5("");
			viewHelper.setImg6("");
			viewHelper.setImg7("");
			viewHelper.setImg8("");
		}
	}
}
